#ifndef __I2C_BUS_H__
#define __I2C_BUS_H__

#include "ipc.h"
#include "device.h"

#ifdef __cplusplus
extern "C" {
#endif

#define I2C_M_WR                0x01  //写
#define I2C_M_RD                0x02  //读
#define I2C_M_ADDR_10BIT        0x04  //10位从机地址
#define I2C_M_NO_START          0x08  //不产生重启信号
#define I2C_M_STOP              0x10  //产生停止信号
#define I2C_M_IGNORE_NAK        0x20  //忽略NAK
#define I2C_M_NO_READ_ACK       0x40  //不产生ACK

/* ioctrl控制命令 */
#define I2C_M_RDWR              0x01

/* I2C总线的幻术 */
#define I2C_M_MAGIC             0x69326362

typedef struct {
    mt_uint16_t addr;
    mt_uint16_t flags;
    mt_uint16_t len;
    mt_uint8_t  *buf;
	mt_int32_t  space;
} mt_i2c_msg_t;

typedef struct {
    mt_size_t num;
    mt_i2c_msg_t *msgs;
}mt_i2c_rdwr_data_t;

struct mt_i2c_bus;
typedef struct {
    mt_size_t (*xfer)(struct mt_i2c_bus *bus, mt_i2c_msg_t msgs[], mt_size_t num);
}mt_i2c_bus_ops_t;

typedef struct mt_i2c_bus{
    mt_device_t      parent;      /* 继承于mt_device_t */
    
    mt_i2c_bus_ops_t *ops;        /* i2c总线操作方法集合 */
    mt_sem_t         lock;            /* 互斥信号量 */
}mt_i2c_bus_t;


typedef struct {
    mt_device_t  parent;      /* 继承于mt_device_t */
    
	mt_uint16_t  addr;        /* 从机地址 */
    mt_i2c_bus_t *bus;        /* 所属总线 */
}mt_i2c_client_t;


mt_ssize_t mt_i2c_bus_transfer(mt_i2c_bus_t *i2c_bus, mt_i2c_msg_t msgs[], mt_size_t num);
mt_err_t mt_i2c_bus_register(mt_i2c_bus_t *i2c_bus, const char *name);

mt_err_t mt_i2c_client_register(mt_i2c_client_t *i2c_client, const char *client_name, const char *bus_name);

#ifdef __cplusplus
}
#endif

#endif
